<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *,
        body {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            background: #222;
        }

        ul,
        li {
            list-style: none;
        }

        a {
            text-decoration: none;
            color: #333;
        }

        a:hover {
            color: lightgreen;
        }

        .container {
            position: relative;
            display: flex;
            justify-content: center;
            align-items: center;
            flex-wrap: wrap;
            gap: 40px;
        }

        .container .card {
            position: relative;
            width: 220px;
            height: 250px;
            background-color: #2a2a2a;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .container .card .percent {
            position: relative;
            width: 150px;
            height: 150px;
        }

        .container .card .percent svg {
            position: relative;
            width: 150px;
            height: 150px;
            /* 海龟朝上 */
            transform: rotate(270deg);
        }

        .container .card .percent svg circle {
            width: 100%;
            height: 100%;
            fill: transparent;
            stroke: #191919;
            stroke-width: 2;
            transform: translate(5px, 5px);
        }

        .container .card .percent svg circle:nth-of-type(2) {
            stroke: var(--clr);
            /* 440是周长 */
        }


        .container .card .dot {
            position: absolute;
            inset: 5px;
            z-index: 10;
            /* 旋转角是算出来的 */
            animation: animateDot var(--time) linear forwards;
        }
        @keyframes animateDot{
            0%{
                transform: rotate(0deg);
            }
            100%{
                transform: rotate(calc(3.6deg * var(--num)));
            }
        } 

        .container .card .dot::before {
            content: '';
            position: absolute;
            top: -5px;
            left: 50%;
            transform: translateX(-50%);
            width: 10px;
            height: 10px;
            border-radius: 50%;
            background: var(--clr);

            box-shadow: 0 0 10px 2px var(--clr),0 0 30px 2px var(--clr);
        }
        .container .card .number{
            position: absolute;
            inset: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            flex-direction: column;
        }
        .container .card .number h2{
            display: flex;
            justify-content: center;
            align-items: center;
            color: #ccc;
            font-weight: 700;
            font-size: 2.5em;
        }
        .container .card .number h2 span{
            font-weight: 300;
            color: #fff;
            font-size: 0.5em;
        }
        .container .card .number p{
            font-weight: 300;
            font-size: 0.75em;
            letter-spacing: 1px;
            color: rgba(255, 255, 255, .75);
        }
    </style>
</head>

<body>
    <div class="container">

        <div class="card">
            <!-- 这里的style是默认值,演示用,可删 -->
            <div class="percent" style="--clr:#cornflowerblue;--num:85">
                <div class="dot"></div>
                <svg>
                    <circle cx="70" cy="70" r="70"></circle>
                    <circle cx="70" cy="70" r="70" stroke-dasharray="0 440"></circle>
                </svg>
                <div class="number">
                    <h2>85<span>%</span></h2>
                    <p>Html</p>
                </div>
            </div>
        </div>

        <div class="card">
            <div class="percent">
                <div class="dot"></div>
                <svg>
                    <circle cx="70" cy="70" r="70"></circle>
                    <circle cx="70" cy="70" r="70" stroke-dasharray="0 440"></circle>
                </svg>
                <div class="number">
                    <h2>85<span>%</span></h2>
                    <p>Html</p>
                </div>
            </div>
        </div>
        
        <div class="card">
            <div class="percent">
                <div class="dot"></div>
                <svg>
                    <circle cx="70" cy="70" r="70"></circle>
                    <circle cx="70" cy="70" r="70" stroke-dasharray="0 440"></circle>
                </svg>
                <div class="number">
                    <h2>85<span>%</span></h2>
                    <p>Html</p>
                </div>
            </div>
        </div>
        
        <div class="card">
            <div class="percent">
                <div class="dot"></div>
                <svg>
                    <circle cx="70" cy="70" r="70"></circle>
                    <circle cx="70" cy="70" r="70" stroke-dasharray="0 440"></circle>
                </svg>
                <div class="number">
                    <h2>85<span>%</span></h2>
                    <p>Html</p>
                </div>
            </div>
        </div>
        
    </div>
    <script>
        window.addEventListener('DOMContentLoaded',function(){
            var percents = document.querySelectorAll('.percent')
        percents.forEach(item => {
            var rgb = getRGB()
            var per = getPercent()
            item.style = '--clr:' + rgb + ';--num:' + per + ';--time:' + (0.015 * per +'s');

            //定时器,更改周长实现动画
            var nowTime = 0;            
            var timer = setInterval(()=>{
                if(nowTime < per){
                    item.children[1].children[1].setAttribute('stroke-dasharray',nowTime * 4.4 + ',440');
                    nowTime ++ ;
                }else{
                    nowTime = 0;
                    clearInterval(timer)
                }
            },15)//这个15和1.5s的时间要对上,也就是--timer * 10的值
        
        item.children[2].children[0].innerHTML = `${per}<span>%</span>`
        item.children[2].children[1].innerHTML = `${rgb}`
        })


        function getRGB() {
            var str = `rgb(${Math.floor(Math.random() * 256)},${Math.floor(Math.random() * 256)},${Math.floor(Math.random() * 256)})`
            return str
        }
        
        function getPercent() {
            var per = Math.floor(Math.random() * 101)
            return per;
        }
        })


    </script>
</body>

</html>